package com.mfc.education.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mfc.education.service.edu.entity.Teacher;
import com.mfc.education.service.edu.entity.vo.TeacherQueryVo;
import com.mfc.education.service.edu.mapper.TeacherMapper;
import com.mfc.education.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mfc.education.common.base.result.R;
import com.mfc.education.service.edu.entity.Course;
import com.mfc.education.service.edu.entity.Teacher;
import com.mfc.education.service.edu.entity.Video;
import com.mfc.education.service.edu.entity.vo.TeacherQueryVo;
import com.mfc.education.service.edu.feign.OssFileService;
import com.mfc.education.service.edu.mapper.CourseMapper;
import com.mfc.education.service.edu.mapper.TeacherMapper;
import com.mfc.education.service.edu.mapper.VideoMapper;
import com.mfc.education.service.edu.service.CourseService;
import com.mfc.education.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.OffsetDateTime;


import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {



    @Autowired
    private OssFileService ossFileService;

    @Autowired
    private CourseMapper courseMapper;



    @Override
    public IPage<Teacher> selectPage(Page<Teacher> teacherPage, TeacherQueryVo teacherVo) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if (teacherVo == null) {
            return baseMapper.selectPage(teacherPage, queryWrapper);
        }


        String name = teacherVo.getName();
        Integer level = teacherVo.getLevel();
        String joinDateBegin = teacherVo.getJoinDateBegin();
        String joinDateEnd = teacherVo.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)) {
            // 左%会使索引失效
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }


        return baseMapper.selectPage(teacherPage, queryWrapper);
    }



    @Override
    public List<Map<String, Object>> selectNameList(String key) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        // 只查询name列
        queryWrapper.select("name");
        queryWrapper.likeRight("name", key);

        return baseMapper.selectMaps(queryWrapper);
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = this.getById(id);
        if (teacher != null) {
            String avatar = teacher.getAvatar();
            if (!StringUtils.isEmpty(avatar)) {
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }

        }
        return false;
    }

    @Override
    public Map<String, Object> selectTeacherInfoById(String id) {

        Teacher teacher = this.getById(id);

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("teacher_id", id);
        List<Course> courseList = courseMapper.selectList(courseQueryWrapper);


        Map<String,Object> map = new HashMap<>();
        map.put("teacher", teacher);
        map.put("courseList",courseList);
        return map;
    }

    @Cacheable(value = "index",key = "'selectHotTeacher'")
    @Override
    public List<Teacher> selectHotTeacher() {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        queryWrapper.last("limit 8");

        return baseMapper.selectList(queryWrapper);
    }

}
